import json
import logging
import traceback
from django.http import JsonResponse
from rest_framework import status
from mgtServer.settings import MEDIA_ROOT
from tools.Tools import check_text, getCurrentUser, createHandleLog, readFile

logger_info = logging.getLogger('info')
res = dict()


def getExcelData(request):
    res.clear()
    try:
        if request.method == 'POST':
            file = request.FILES.get('file')
            files_address = MEDIA_ROOT / file.name
            file_byte = open(files_address, 'wb+')
            for chunk in file.chunks():
                file_byte.write(chunk)
            file_byte.close()
            read_content = readFile(file.name)
            res['status'] = 200
            res['data'] = read_content
            res['message'] = '请求成功'
            return JsonResponse(res, status=status.HTTP_200_OK)
    except Exception as e:
        print(e)
        logger_info.error(f'当前接口：{request.path} 异常，异常内容为:{traceback.format_exc()}')
        res['status'] = 200
        res['message'] = '请求错误!'
        return JsonResponse(res, status=status.HTTP_400_BAD_REQUEST)